#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>

namespace ns_sock {
    #define backlog 5
    class Sock {
    public:
        static int Socket() {
            int sock = socket(AF_INET, SOCK_STREAM, 0);
            if (sock < 0) {
                std::cout << "socket error" << std::endl;
                exit(1);
            }
            return sock;
        }
        
        static void Bind(const int sock, uint16_t port = 8080) {
           struct sockaddr_in local;
           bzero(&local, sizeof(local));
           local.sin_family = AF_INET;
           local.sin_port = htons(port);
           local.sin_addr.s_addr = INADDR_ANY;
           if (bind(sock, (struct sockaddr *)&local, sizeof(local)) <  0) {
                std::cerr << "bind error" << std::endl;
                exit(2);
           }
        }
        static void Listen(const int sock) {
            if (listen(sock, backlog) < 0) {
                std::cerr << "listen error" << std::endl;
                exit(3);
            }
        }
    };
}
